package net.zjitc.action.Admin;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.damain.Admin.Admin;
import net.zjitc.damain.User.User;
import net.zjitc.service.Admin.AdminService;
import net.zjitc.service.User.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/6.
 */
@Controller
@Scope("prototype")
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {
    //定义一个log变量
    private Log log = LogFactory.getLog(this.getClass());

    private Admin Model = new Admin();

    public Admin getModel() {
        return Model;
    }

    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;

    private Integer uid;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    /*登录界面*/
    public String loginUI(){
        return "loginUI";
    }
    /*登录请求*/
    public String login(){
        //如果有用户返回登录成功
        Admin admin=adminService.findByAdminnamePassword(Model.getUsername(),Model.getPassword());
        ActionContext.getContext().getSession().put("admin",admin);
        if (admin!=null){
            log.debug("登录成功");
            this.addActionMessage("登录成功");
            return "loginSuccess";
        }
        //否则返回登录失败
        this.addActionError("用户名或密码错误");
        return "loginFail";
    }
    public String findAllUser(){
        List<User> userList = userService.findAll();
        ActionContext.getContext().put("userList",userList);
        return "findAll";
    }
    public String deleteUser(){
        User user = userService.getById(uid);
        userService.Delete(user);
        return "delete";
    }

}
